<template>
  <el-form label-width="100px">
    <el-form-item label="补贴名称">
      <el-input v-model="subsidy.name"/>
    </el-form-item>
    <el-form-item label="补贴金额">
      <el-input v-model="subsidy.price"/>
    </el-form-item>
    <el-form-item label="时间">

      <el-date-picker v-model="subsidy.timestamp.timestamp"
                      format="yyyy-MM-dd HH:mm"
                      placeholder="选择时间"
                      style="width: 100%;"
                      type="datetime"
                      value-format="timestamp"
      />

    </el-form-item>
    <el-form-item style="text-align: right">
      <el-button v-if="selectSubsidy" type="primary" @click="updateSubsidy">修改</el-button>
      <el-button v-if="!selectSubsidy" type="primary" @click="addSubsidy">添加</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import {AddSubsidy, UpdateSubsidy} from "../../../../network/subsidyApi";
import {copyObj} from "../../../../common/utils";

export default {
  name: "subsidyForm",
  data() {
    return {
      subsidy: {
        companyUuid: this.$route.query.id,
        timestamp: {},
      },
    }
  },
  methods: {
    addSubsidy() {
      this.subsidy.timestamp.timestamp = this.subsidy.timestamp.timestamp / 1000
      AddSubsidy(this.subsidy).then(res => {
        if (res.code === 2000) {
          this.subsidy.price = null,
              this.subsidy.name = null,
              this.subsidy.timestamp.timestamp = null
          this.$emit("success")
        }
        if (res.code !== 2000) {
          this.$message({
            message: res.message,
            type: "error"
          })
        }
      })
    },
    updateSubsidy() {
      this.subsidy.timestamp.timestamp = this.subsidy.timestamp.timestamp / 1000
      UpdateSubsidy(this.subsidy).then(res => {
        if (res.code === 2000) {
          this.$emit("success")
        }
        if (res.code !== 2000) {
          this.$message({
            message: res.message,
            type: "error"
          })
        }
      })
    },
  },
  watch: {
    'selectSubsidy': {
      handler() {
        this.subsidy = copyObj(this.selectSubsidy)
        this.subsidy.timestamp.timestamp = this.subsidy.timestamp.timestamp * 1000
      }
    }
  },
  mounted() {
    if (this.selectSubsidy) {
      this.subsidy = copyObj(this.selectSubsidy)
      this.subsidy.timestamp.timestamp = this.subsidy.timestamp.timestamp * 1000

    }
  },
  props: ['selectSubsidy'],
}
</script>

<style scoped>

</style>